<template>
  <div class="page">
    <!-- 左边前景图案 -->
    <div class="front-img" />
    <!-- 登录表单 -->
    <div class="container">
      <el-row>
        <el-col :xs="1" :sm="1" :md="12" :lg="13" :xl="16"><div class="grid-content"></div></el-col>
        <el-col :xs="22" :sm="22" :md="11" :lg="10" :xl="7">
          <div class="grid-content">
            <el-form ref="loginForm" :model="formData" :rules="formRules" class="login-form" autocomplete="on" label-position="left">
              <div class="login_title">
                <div class="title_word">{{ loginTitle }}</div>
                <img class="login-logo" src="~@/assets/images/logo.png" alt="" />
              </div>
              <el-form-item>
                <span class="svg-container"> <svg-icon icon-class="date" /> </span>
                <el-input size="small" v-model="formData.time" readonly placeholder="今天的日期" />
              </el-form-item>
              <el-form-item prop="DbName">
                <span class="svg-container"> <svg-icon icon-class="money" /> </span>
                <el-select size="small" name="DbName" v-model="formData.DbName" placeholder="请选择" popper-class="pop-box">
                  <el-option v-for="item in dbList" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
              <el-form-item prop="account">
                <span class="svg-container"> <svg-icon icon-class="people" /> </span>
                <el-input
                  size="small"
                  ref="account"
                  v-model="formData.account"
                  placeholder="请输入您的账号"
                  name="account"
                  type="text"
                  clearable
                  tabindex="1"
                  autocomplete="on"
                />
              </el-form-item>

              <el-tooltip v-model="capsTooltip" content="注意：大写状态已开启" placement="top-start" manual>
                <el-form-item prop="password">
                  <span class="svg-container"> <svg-icon icon-class="password" /> </span>
                  <el-input
                    size="small"
                    :key="passwordType"
                    ref="password"
                    v-model="formData.password"
                    :type="passwordType"
                    placeholder="请输入您的密码"
                    name="password"
                    tabindex="2"
                    clearable
                    autocomplete="on"
                    @keyup.native="checkCapslock"
                    @blur="capsTooltip = false"
                    @keyup.enter.native="handleLogin"
                  />
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                  </span>
                </el-form-item>
              </el-tooltip>

              <el-button :loading="loading" type="primary" class="login-button" @click.native.prevent="handleLogin">登录</el-button>

              <!-- <div style="position: relative">
          <div class="tips">
            <span>account : admin</span>
            <span>password : any</span>
          </div>
          <div class="tips">
            <span style="margin-right: 18px">account : editor</span>
            <span>password : any</span>
          </div>

          <el-button class="thirdparty-button" type="primary" @click="showDialog = true"> 三方登录 </el-button>
        </div> -->
            </el-form>
            <div class="footer_box">
              <div class="qr_box"><img class="qr" src="~@/assets/images/qr.png" alt="" /></div>
              <div class="right">
                <p v-for="(item, inx) in infoList" :key="inx">{{ item }}</p>
                <p>
                  {{ portalSite.title }} : <a href="portalSite.url" :title="portalSite.title">{{ portalSite.url }}</a>
                </p>
              </div>
            </div>
          </div>
        </el-col>
        <el-col :xs="1" :sm="1" :md="1" :lg="1" :xl="1"><div class="grid-content"></div></el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { validaccount } from '@/utils/validate'
import SocialSign from './components/SocialSignin'
import { getDbList } from '@/api/system/database'
import { loginTitle } from '@/settings'
export default {
  name: 'Login',
  components: { SocialSign },
  data() {
    const validateaccount = (rule, value, callback) => {
      callback()

      // if (!validaccount(value)) {
      //   callback(new Error('Please enter the correct user name'))
      // } else {
      //   callback()
      // }
    }
    const validatepassword = (rule, value, callback) => {
      callback()
      // if (value.length < 6) {
      //   callback(new Error('The password can not be less than 6 digits'))
      // } else {
      //   callback()
      // }
    }
    const validateDbName = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请先选择套账'))
      } else {
        callback()
      }
    }
    return {
      loginTitle,
      infoList: ['客户服务中心', '联系电话：15018388417 522844723', '版本：9400001.22.5.297'],
      portalSite: { title: '官方网站', url: 'http://www.lanhuasoft.com' },
      formData: {
        account: 'admin',
        password: '123',
        PhoneName: '',
        DbName: 'LH0001',
        IMEI: '',
      },
      dbList: [],
      formRules: {
        DbName: [{ required: true, trigger: 'blur', validator: validateDbName }],
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入账号密码', trigger: 'blur' }],
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true,
    },
  },
  created() {
    // console.log(this.$guid())
    // console.log(this.$dayjs().format('YYYY-MM-DD'))
    // window.addEventListener('storage', this.afterQRScan)
    this.formData.time = this.$dayjs().format('YYYY-MM-DD')
  },
  mounted() {
    if (this.formData.account === '') {
      this.$refs.account.focus()
    } else if (this.formData.password === '') {
      this.$refs.password.focus()
    }
    // this.getDbList()
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    async getDbList() {
      try {
        let res = await getDbList()
        this.dbList = res.data.map((e) => {
          return { label: e, value: e }
        })
      } catch (e) {
        console.error('login > getDbList err:', e)
      }
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async handleLogin() {
      try {
        this.loading = true
        await this.$refs.loginForm.validate()
        const res = await this.$store.dispatch('user/login', this.formData)
        this.loading = false
        this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
      } catch (e) {
        console.log('输入不完整')
      }
      // this.$refs.loginForm.validate((valid) => {
      //   if (valid) {
      //     this.loading = true
      //     this.$store
      //       .dispatch('user/login', this.formData)
      //       .then(() => {
      //         this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
      //         this.loading = false
      //       })
      //       .catch(() => {
      //         this.loading = false
      //       })
      //   } else {
      //     console.log('error submit!!:', valid)
      //     return false
      //   }
      // })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  },
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .page .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.page {
  .el-select {
    width: 90%;
  }
  .el-select > .el-input {
    width: 100%;
  }

  .el-input {
    display: inline-block;
    height: 47px;
    width: 82%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $bg;
      height: 47px;
      caret-color: $bg;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(0, 0, 0, 0.2);
    border-radius: 6px;
    box-sizing: border-box;
    color: #454545;
  }
}
.pop-box {
  height: 200px;
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.page {
  min-width: 800px;
  width: 100%;
  height: 100vh;
  background-image: url('~@/assets/images/login_bg.png');
  background-size: 100% 100%;
  position: fixed;
  position: relative;
  top: 0;
  left: 0;
  .front-img {
    background-image: url('~@/assets/images/login_front.png');
    background-size: cover;
    position: absolute;
    top: calc(50vh - 260px);
    left: 180px;
    min-width: 737px;
    height: 533px;
  }
  .svg-container {
    padding: 5px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .show-pwd {
    padding: 2px 4px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
.container {
  width: 100%;
  height: 100%;
  // background: #283443;
}
.login-form {
  z-index: 10;
  background-color: #fff;
  border-radius: 21px;
  width: 460px;
  padding: 30px 47px;
  height: 480px;
  .login_title {
    color: #0595f2;
    font-size: 26px;
    margin-bottom: 23px;
    font-weight: 800;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .title_word {
      position: relative;
    }

    .title_word::after {
      content: '';
      width: 89px;
      height: 13px;
      background: #53a6ff;
      opacity: 0.3;
      position: absolute;
      bottom: 0px;
      left: 2px;
    }
    .login-logo {
      width: 65px;
    }
  }
}

.footer_box {
  margin-top: 10px;
  width: 460px;
  height: 145px;
  box-shadow: 0px 0px 49px 0px rgba(5, 149, 242, 0.35);
  background-color: rgba(45, 88, 131, 0.2);
  border-radius: 21px;
  display: flex;
  align-items: center;

  .qr {
    width: 95px;
    height: 98px;
    margin: 0 18px;
    border-radius: 8px;
  }

  .right {
    color: rgba(255, 255, 255, 0.8);
    font-size: 14px;
    height: 98px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 3px 0;

    p {
      margin: 0;
    }
  }
}
.grid-content {
  height: 97vh;
  display: flex;
  align-content: center;
  justify-content: center;
  flex-direction: column;
  flex-wrap: wrap;
}
.login-button {
  width: 100%;
  height: 47px;
  margin-bottom: 30px;
}
</style>
